/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 *  The greatest achievement is selflessness. The greatest worth is self-mastery.
 *  The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 *  The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 *  The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 *  The greatest goodness is a peaceful mind. The greatest patience is humility.
 *  The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 *  The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 * 
 * @author Dimax Pradi
 */
package org.javaongems.gwk.client;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;

public class SimpleFormPanel extends Composite implements ClickListener {
  public interface Listener {
    void onSubmit(String btnTitle);
    void onCancel();
  }

  private DockPanel container = new DockPanel();
  private FlexTable flexTable = new FlexTable();
  private FlowPanel fpButton = new FlowPanel();

  private Map labelMap = new HashMap();
  private Map textBoxMap = new HashMap();
  private Map passwordTextBoxMap = new HashMap();
  private Map buttonMap = new HashMap();
  private int row, col = 0;

  SimpleFormPanel.Listener listener;

  public SimpleFormPanel() {
    initWidget(container);

    container.add(flexTable, DockPanel.CENTER);
    container.add(fpButton, DockPanel.SOUTH);
  }

  public void addInputText(String name, int maxLength, int size) {
    Label label = new Label(name);
    label.setTitle(name);

    TextBox textBox = new TextBox();
    textBox.setTitle(name);
    textBox.setMaxLength(maxLength);
    textBox.setWidth(size + "em");

    flexTable.setWidget(row, col, label);
    flexTable.setWidget(row, col + 1, textBox);
    row++;

    labelMap.put(name, label);
    textBoxMap.put(name, textBox);
  }

  public void addPasswordInputText(String name, int size) {
    Label label = new Label(name);
    label.setTitle(name);

    PasswordTextBox textBox = new PasswordTextBox();
    textBox.setTitle(name);
    textBox.setWidth(size + "em");

    flexTable.setWidget(row, col, label);
    flexTable.setWidget(row, col + 1, textBox);
    row++;

    labelMap.put(name, label);
    passwordTextBoxMap.put(name, textBox);
  }

  public void addButton(String name) {
    Button btn = new Button(name);
    btn.setTitle(name);
    btn.addClickListener(this);
    fpButton.add(btn);
    buttonMap.put(name, btn);
  }
  
  public void clearForm() {
    Collection col = textBoxMap.values();
    for (Iterator it = col.iterator(); it.hasNext(); ) {
      TextBoxBase tb = (TextBoxBase) it.next();
      tb.setText("");
    }
    col = passwordTextBoxMap.values();
    for (Iterator it = col.iterator(); it.hasNext(); ) {
      TextBoxBase tb = (TextBoxBase) it.next();
      tb.setText("");
    }
  }

  public TextBox getTextBox(String name) {
    return (TextBox) textBoxMap.get(name);
  }

  public PasswordTextBox getPasswordTextBox(String name) {
    return (PasswordTextBox) passwordTextBoxMap.get(name);
  }

  public Button getButton(String name) {
    return (Button) buttonMap.get(name);
  }

  public void setListener(SimpleFormPanel.Listener listener) {
    this.listener = listener;
  }

  public void onClick(Widget sender) {
    if (listener == null) {
      return; //TODO should log error.
    }
    if (sender == buttonMap.get("Cancel")) {
      listener.onCancel();
    } else {
      listener.onSubmit(sender.getTitle());
    }
  }

}
